<?php
	require_once("utils.php");
	require_once("obj/taskpriority.php");
	$obj = TaskPriority::getFromUrl();
	if(isset($_POST["btnSubmit"])){ 
		if($_POST["btnSubmit"] == "Save") {
			$obj->Name = $_POST["txtName"];
			$obj->Description = $_POST["txtDescription"];
			$obj->Position = $_POST["txtPosition"];
			$obj->save();
		}
		else if($_POST["btnSubmit"] == "Delete") { 
			$obj->delete();
		}
		
		$err = error_get_last();
		if (empty($err)) {
			$u = Utils::makeTaskPriorityUrl();
			header("Location:$u");
		}
	}
?>

<form method="post" action="<?php echo Utils::makeTaskPriorityDetailUrl($obj->TaskPriorityId);?>">
	<table cellpadding="5" cellspacing="0" border="0">
		<tr>
			<td>Name:</td>
			<td><input type="text" name="txtName" id="txtName" value="<?php echo $obj->Name; ?>" /></td>
		</tr>
		<tr>
			<td valign="top">Description:</td>
			<td>
				<textarea name="txtDescription" id="txtDescription"><?php echo $obj->Description; ?></textarea>
			</td>
		</tr>
		<tr>
			<td>Position:</td>
			<td>
				<input type="text" name="txtPosition" id="txtPosition" value="<?php echo $obj->Position; ?>" />
			</td>
		</tr>
		<tr>
			<td colspan="2">
				<input name="btnSubmit" type="submit" value="Save" onclick="return validateDetails();" />
				<input name="btnSubmit" type="submit" value="Delete" />
				<input type="button" value="Cancel" onclick="javascript:history.back();" />
			</td>
		</tr>
	</table>
</form>

<script language="javascript" type="text/javascript">	
	function validateDetails() { 		
		var msg = "";
		var ok = true;		
		
		if(!validateRequired('txtName')) {
			msg += "Name is required\n";
			ok = false;
		}
		
		if(typeof(0) != typeof(parseInt(getE('txtPosition').value))){
			msg += "Position must be an integer\n";
			ok = false;
		}
	
		if(!ok) {
			alert(msg);
		}
		return ok;
	}
</script>